﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// wuliu_list 的摘要说明
    /// </summary>
    public class wuliu_list : IHttpHandler, IRequiresSessionState
    {

        protected int totalCount = 0;
        protected int page = 1;
        protected int pageSize = 0;
        public void ProcessRequest(HttpContext context)
        {
            this.pageSize = 15;
            this.page = DTRequest.GetQueryInt("page", 1);

            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            //获取供应信息
            BLL.wuliu bll = new BLL.wuliu();
            DataSet ds = bll.GetList(this.pageSize, this.page, "", "id desc", out this.totalCount);
            int total_page = this.totalCount / this.pageSize;
            strTxt.Append("\"total_page\":" + total_page.ToString() + ",");
            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"wuliu_list\":[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"type_id\":" + dr["type_id"]);
                    if (Convert.ToInt32(dr["type_id"]) == 1)
                    {
                        strTxt.Append(",\"type_name\":\"求车\"");
                    }
                    else
                    {
                        strTxt.Append(",\"type_name\":\"求货\"");
                    }
                    strTxt.Append(",\"title\":\"" + dr["start_address"] + "->" + dr["end_address"] + "\"");
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr["add_time"])) + "\"");
                    if (Convert.ToInt32(dr["user_id"]) > 0)
                    {
                        string mobile = new BLL.users().GetUserName(Convert.ToInt32(dr["user_id"]));
                        strTxt.Append(",\"mobile\":\"" + mobile + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"mobile\":\"" + dr["mobile"] + "\"");
                    }
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            else
            {
                strTxt.Append("\"wuliu_list\":[]");
            }
            strTxt.Append("}");

            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}